package Java7_Concurrency_Cookbook.concurrent_collection_framework.concurrent_linked_deque;

import java.util.concurrent.ConcurrentLinkedDeque;

/**
 * ConcurrentLinkedDeque.getFirst(), getLast() 返回的元素不会从列表中移除.如果列表为空, 抛出异常.<br/>
 * ConcurrentLinkedDeque.peek(), peekFirst(), peekLast() 返回的元素不会从列表中移除.如果列表为空, 返回NULL.<br/>
 * ConcurrentLinkedDeque.remove(), removeFirst(), removeLast() 返回的元素将从列表中删除. 如果列表为空, 抛出异常.
 * @author lixiangke
 * @date 2017/11/23
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<>();
        Thread[] threads = new Thread[100];
        for (int i = 0; i < 100; i++) {
            AddTask task = new AddTask(list);
            threads[i] = new Thread(task);
            threads[i].start();
        }
        System.out.printf("Main: %d AddTask Threads have been launched\n", threads.length);

        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        System.out.printf("Main: Size of the list: %d\n", list.size());

        for (int i = 0; i < threads.length; i++) {
            PollTask task = new PollTask(list);
            threads[i] = new Thread(task);
            threads[i].start();
        }
        System.out.printf("Main: %d PollTask threads have been launched.\n", threads.length);

        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        System.out.printf("Main: Size of the List: %d\n", list.size());
    }
}
